package com.F02.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.F02.action.F020104Action;
import com.F02.actionform.F020104ActionForm;
import com.F02.dao.F020104DAO;
import com.action.BaseAction;
import com.config.Constants;
import com.dao.common.DbOperationExt;
import com.exception.SelectException;
import com.info.T_S_YHCZXXinfo;
import com.util.SessionUtils;

/**
 * @leip
 * @材料码表维护
 * @功能：初始化，查询，新增，修改，删除�?
 * @规范：execute()方法制作判断转发，不处理具体业务�? @ ACTION中所有�?�辑判断对应的具体业务方法的命名规则
 * do+静态参数，例如:operationType=init，实现方法为doINIT(). @ 涉及到连接数据库的方法全部写在DAO中�
 * @操作记录：对于insert，update，delete操作系统�?要记录�??
 * @调试信息打印使用LOG.INFO();
 * @记得加注释

 */

public class F020104Action extends BaseAction{
	/** 记录操作日志 */
	Logger log = Logger.getLogger(F020104Action.class);

	/** 操作记录INFO */
	T_S_YHCZXXinfo t_YhczxxInfo = null;

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		/** 获得actionForm对象 */
		F020104ActionForm actionForm = (F020104ActionForm) form;
		/** 获得operationType值�?�做执行判断�? */
		String operationType = actionForm.getOperationType();
		log.info("operationType = " + operationType);
		/** 获取实例 */
		F020104DAO dao = new F020104DAO();
		/**页面语言选择提示*/
		String language = SessionUtils.getLocale(request);
		actionForm.setLanguage(language);
		try {
			/** 生成操作记录语句 */
			t_YhczxxInfo = setYhczxx(request, response, operationType,
					"F020105");
			/**
			 * @页面初始�?
			 * @统一参数：operationType=init
			 * @规范：左树进入连接页面不执行查询�?
			 */
			if (Constants.INIT.equals(operationType)) {
				this.doINIT(actionForm,dao);
				return mapping.findForward(Constants.List);
			}
			/**
			 * @查询功能
			 * @统一参数：operationType=queryList
			 * @规范：查询后回显查询条件。如果查询结果无记录，在页面给出提示�?
			 */
			if (Constants.QUERYLIST.equals(operationType)) {
				this.doQUERYLIST(actionForm, dao, request);
				return mapping.findForward(Constants.List);
			}
			/**
			 * @新增功能
			 * @统一参数：operationType=add
			 * @规范：进入新增页面�??
			 */
			if (Constants.ADD.equals(operationType)) {
				this.doADD(actionForm, dao, request);
				return mapping.findForward(Constants.detail);
			}
			/**
			 * @新增保存功能
			 * @统一参数：operationType=insert
			 * @规范：新增保存后，返回查询页面，更新记录，给出用户提示信息�?��??
			 */
			if (Constants.SAVE_INSERT.equals(operationType)) {
				this.doSAVE_INSERT(actionForm, dao, request);
				return mapping.findForward(Constants.List);
			}
			/**
			 * @编辑功能
			 * @统一参数：operationType=edit
			 * @规范：进入修改页面�??
			 */
			if (Constants.EDIT.equals(operationType)) {
				this.doEDIT(actionForm, dao, request);
				return mapping.findForward(Constants.detail);
			}
			/**
			 * @编辑保存功能
			 * @统一参数：operationType=update
			 * @规范：修改保存后，返回查询页面，更新记录，给出用户提示信息�??
			 */
			if (Constants.SAVE_UPDATE.equals(operationType)) {
				this.doSAVE_UPDATE(actionForm, dao, request);
				return mapping.findForward(Constants.List);
			}
			/**
			 * @删除功能
			 * @统一参数：operationType=delete
			 * @规范：更新记录，给出用户提示信息�?
			 */
			if (Constants.DELETE.equals(operationType)) {				
				this.doDELETE(actionForm, dao, request);
				return mapping.findForward(Constants.List);
			}
			/**
			 * @返回List页面
			 * @统一参数：operationType=back
			 * @规范：返回LIST页面，显示查询结果

			 */
			if (Constants.BACK.equals(operationType)) {
				this.doActionForm(actionForm, dao, request);
				return mapping.findForward(Constants.List);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (dao != null)
				dao.disConnect();
		}
		return mapping.findForward(Constants.INIT);
	}

	/**
	 * @初始�?
	 */
	public void doINIT(F020104ActionForm actionForm,F020104DAO dao) throws SelectException {				
		actionForm.setFamilyList(dao.List_pc_material_family());
		actionForm.setCurrentPage(1);
	}

	/**
	 * @查询
	 */
	private void doQUERYLIST(F020104ActionForm actionForm, F020104DAO dao,
			HttpServletRequest request) throws SelectException {
		List list = dao.QUERYLIST(actionForm, request);
		if(list!=null&&list.size()>0){
			actionForm.setList(list);
		}else{
			actionForm.setList(list);
			actionForm.setErrorFlag("2");
		}
	}

	/**
	 * @新增
	 */
	private void doADD(F020104ActionForm actionForm, F020104DAO dao,
			HttpServletRequest request) throws SelectException {		
		actionForm.setCode("");
		actionForm.setName("");			
		actionForm.setDescription("");
		actionForm.setFamilyCode("");
		actionForm.setMeasureList(dao.List_t_measure_units());
		actionForm.setMeasureCode("");
		actionForm.setOperationType(Constants.SAVE_INSERT);
	}

	/**
	 * @新增保存
	 * @执行新增后，清空actionForm，执行查询操作，返回查询界面。

	 */
	private void doSAVE_INSERT(F020104ActionForm actionForm, F020104DAO dao,
			HttpServletRequest request) throws SelectException {
		dao.SAVE_INSERT(actionForm, t_YhczxxInfo);
		this.doActionForm(actionForm, dao, request);
	}

	/**
	 * @编辑
	 */
	private void doEDIT(F020104ActionForm actionForm, F020104DAO dao,
			HttpServletRequest request) throws SelectException {
		dao.EDIT(actionForm, request);
		actionForm.setMeasureList(dao.List_t_measure_units());
		actionForm.setOperationType(Constants.SAVE_UPDATE);
	}

	/**
	 * @编辑保存
	 */
	private void doSAVE_UPDATE(F020104ActionForm actionForm, F020104DAO dao,
			HttpServletRequest request) throws SelectException {
		dao.SAVE_UPDATE(actionForm, t_YhczxxInfo);
		this.doActionForm(actionForm, dao, request);
	}

	/**
	 * @删除
	 */
	private void doDELETE(F020104ActionForm actionForm, F020104DAO dao,
			HttpServletRequest request) throws SelectException {
		dao.DELETE(actionForm, t_YhczxxInfo, request);
		this.doActionForm(actionForm, dao, request);
	}

	/**
	 * @throws SelectException
	 * @清空actionForm
	 * @执行查询回显操作
	 */
	private void doActionForm(F020104ActionForm actionForm, F020104DAO dao,
			HttpServletRequest request) throws SelectException {
		actionForm.setId("");
		actionForm.setCode("");
		actionForm.setName("");	
		actionForm.setFamilyCode("");
		actionForm.setMeasureCode("");
		actionForm.setDescription("");		
		actionForm.setOperationType("");
		actionForm.setErrorFlag("");
		this.doQUERYLIST(actionForm, dao, request);
	}
}
